package com.itgungnir.diyshuo.fragment.zhinan_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.adapter.ListAdapter;
import com.itgungnir.diyshuo.base.BaseFragment;
import com.itgungnir.diyshuo.bean.GoodsBean;
import com.stx.xhb.xbanner.XBanner;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 展示“指南”页面的Fragment
 */
public class ZhiNanFragment extends BaseFragment implements ZhinanContract.View {

    PullToRefreshListView znPlv;
    private ZhinanPresenter presenter;
    private ListView mLv;
    private XBanner xbanner;
    public int getLayoutId() {
        return R.layout.fragment_zhinan;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        znPlv= (PullToRefreshListView) view.findViewById(R.id.zn_plv);
        //实例化ZhinanPresenter,调用方法加载数据
        presenter = new ZhinanPresenter(this);
        presenter.getZhinanFromNet();
        //pullToRefreshListView适配数据之前需调用此方法获得listview
        mLv= znPlv.getRefreshableView();
    }

    //数据加载成功后，将数据适配给pullToRefreshListView
    public void onGetZhinanSuccess(List<GoodsBean> goodsBeanList, final List<Integer> list) {
        View view2=LayoutInflater.from(getActivity()).inflate(R.layout.header_znlv,null);
        xbanner= (XBanner) view2.findViewById(R.id.zn_vp);
        xbanner.setData(list);
        XBanner.XBannerAdapter xadapter = new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, View view, int position) {
                Glide.with(getActivity()).load(list.get(position)).into((ImageView) view);
            }
        };
        xbanner.setmAdapter(xadapter);

        mLv.addHeaderView(view2);
        //适配数据
        ListAdapter adapter = new ListAdapter(goodsBeanList, getActivity());
        mLv.setAdapter(adapter);
    }

    //加载数据失败后的提示消息
    public void onGetZhinanFail(String msg) {
        Toast.makeText(getActivity(),"加载数据失败。。。",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClickListener() {
        
    }
}